import Schema from '../common/async-validator/index';
import validator from './validator';
export default {
    data() {
        return {
            schema: '',
            tipFun: {
                message: 'showMessage',
                toast: 'showToast'
            },
            tipContent: {
                message: 'content',
                toast: 'title'
            },
            errorText: '',
            errors: []
        };
    },
    mixins: [validator],
    props: {
        rules: {
            type: [Object, Array],
            default: []
        },
        tipType: {
            type: String,
            default: 'toast',
            options: ['toast', 'message', 'text']
        }
    },
    methods: {
        initRules() {
            const { rules: t } = this;
            t &&
                ('[object Object]' === Object.prototype.toString.call(t) && (this.rules = [t]),
                this.rules.forEach((t) => {
                    t.trigger ? 'string' != typeof t.trigger || (t.trigger = [t.trigger]) : (t.trigger = []);
                }));
        },
        getNeedValidateRule(t) {
            const e = this.name,
                { rules: a } = this;
            if (!a) return;
            const r = t ? a.filter((e) => e.trigger.indexOf(t) > -1) : a,
                s = new Schema({
                    [e]: r
                });
            return (
                this.setData({
                    schema: s
                }),
                r
            );
        },
        validatorData(t, e) {
            const { tipType: a, tipFun: r, tipContent: s } = this;
            this.getNeedValidateRule(e) &&
                (Object.getOwnPropertyNames(t).forEach((e) => {
                    '' === t[e] && (t[e] = void 0);
                }),
                this.schema.validate(t, (t) => {
                    if (
                        (this.setData({
                            errors: t || []
                        }),
                        this.$emit('linvalidate', {
                            detail: {
                                errors: t,
                                isError: !!t
                            }
                        }),
                        t && a)
                    ) {
                        const e = r[a],
                            i = s[a];
                        return 'text' === a
                            ? (this.setData({
                                  errorText: t[0].message
                              }),
                              t)
                            : uni.lin && uni.lin[e]
                            ? (uni.lin[e] &&
                                  uni.lin[e]({
                                      [i]: t[0].message,
                                      duration: 1500,
                                      mask: !1
                                  }),
                              t)
                            : (uni.showToast({
                                  icon: 'none',
                                  title: `请在页面内引入${a}组件`
                              }),
                              t);
                    }
                    !t &&
                        a &&
                        this.setData({
                            errorText: ''
                        });
                }));
        }
    }
};
